import json
import requests
import math

import coin2.support.product_info.spot_binance_v1 as binance_pi


def get_product_market_info(symbol):
  url = 'https://api.binance.com/api/v3/ticker/24hr?symbol=' + symbol
  resp = requests.get(url)
  data = resp.json()
  volume = float(data.get('quoteVolume', 0))
  last_price = float(data.get('lastPrice', 0))
  return {
    'volume': volume,
    'last_price': last_price,
  }


def is_skip(s):
  excludes = ['BUSD', 'USDC', 'UP', 'DOWN', 'PAX', 'TUSD']
  for word in excludes:
    if word in s:
      return True
  return False



def gen_usdt_products_info():
  binance_pi_data = binance_pi.generate()

  products = []
  for each in binance_pi_data:
    if is_skip(each['base']):
      continue

    if each['quote'] == 'USDT':
      products.append(each)

  for prod in products:
    symbol = prod['native_symbol']
    info = get_product_market_info(symbol)
    prod.update(info)
  return sorted(products, key=lambda x: x['volume'], reverse=True)


def gen_config():
  result = {
    "mea": "Spot.Binance.v1",
    "products": {},
    "ref_mea": "Futures.Binance.v1",
    "key_filepath": "trade/xunke18/binance/fut_trade_key.json",
    "strategy_name": "binance_spot_eos_mm",
    "rest_query_config": {
      "query_account_balance_period_sec": 5,
      "query_open_orders_period_sec": 2,
      "query_cancel_orders_period_sec": 5,
      "query_fill_period_sec": 5
    }
  }
  data = gen_usdt_products_info()
  data = data[:50]

  for each in data:
    entry = {}
    base = each['base']
    last_price = each['last_price']
    lot_size = 12. / last_price

    result['products'][base] = entry
    entry["trade_symbol"] = "Spot:Binance:%s-USDT" % base
    entry["ref_symbol"] = "Futures:Binance:%s-USDT.PERPETUAL" % base
    entry["basis_ma_window"] = 300
    entry["lot_size"] = lot_size
    entry["edge_bp"] = 10
    entry["min_pos"] = 0
    entry["max_pos"] = lot_size * 5
    entry["order_update_period_sec"] = 2
    entry["max_posting_period_sec"] = 20

  print(json.dumps(result, indent=2))


def gen_config1():
  result = {
    "mea": "Futures.Binance.v1",
    "products": {},
    "ref_mea": "Spot.Binance.v1",
    "key_filepath": "trade/xunke18/binance/fut_trade_key.json",
    "strategy_name": "binance_futures_mm",
    "rest_query_config": {
      "query_account_balance_period_sec": 5,
      "query_open_orders_period_sec": 2,
      "query_cancel_orders_period_sec": 5,
      "query_fill_period_sec": 5
    }
  }
  data = gen_usdt_products_info()
  data = data[:30]

  for each in data:
    entry = {}
    base = each['base']
    last_price = each['last_price']
    lot_size = 12. / last_price

    result['products'][base] = entry
    entry["trade_symbol"] = "Futures:Binance:%s-USDT.PERPETUAL" % base
    entry["ref_symbol"] = "Spot:Binance:%s-USDT" % base
    entry["basis_ma_window"] = 300
    entry["lot_size"] = lot_size
    entry["edge_bp"] = 10
    entry["min_pos"] = 0
    entry["max_pos"] = lot_size * 5
    entry["order_update_period_sec"] = 2
    entry["max_posting_period_sec"] = 20

  print(json.dumps(result, indent=2))



gen_config1()
